/*
 * @(#)TestsetTranslator.java
 *
 *
 *
 * ver 1.0 Sep 4, 2007 ggranum
 */
package org.testfire.realm.view.json.translator;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.testfire.realm.control.auth.user.UserDBAO;
import org.testfire.realm.control.exception.UserNotFoundException;
import org.testfire.realm.model.entities.User;
import org.testfire.realm.model.entities.testfire.AgentEntity;
import org.testfire.realm.util.json.JsonField;
import org.testfire.realm.util.json.Tools;

// eof: TestsetTranslator.java

/**
 * Application Scope
 */
public class AgentTranslator implements JsonTranslator<AgentEntity>
{

    static final List<JsonField> ALL_FIELDS = new ArrayList<JsonField>();

    static
    {
        ALL_FIELDS.add( new JsonField( "id", JsonField.TYPE_INT ) );
        ALL_FIELDS.add( new JsonField( "name", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "description", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "host", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "userName", JsonField.TYPE_STRING ) );
        ALL_FIELDS.add( new JsonField( "allowNonUserTests", JsonField.TYPE_BOOLEAN ) );
        ALL_FIELDS.add( new JsonField( "hastestset", JsonField.TYPE_STRING ) );
    }

    public String toJson( AgentEntity object )
    {
        return Tools.getJsonString( object, ALL_FIELDS );
    }

    public String toJson( AgentEntity object, String fields ) throws JSONException
    {
        List<JsonField> jFields = JsonField.parse( fields );
        JSONObject obj = new JSONObject();
        for ( JsonField f : jFields )
        {

            if ( f.getName().equals( "id" ) )
            {
                obj.put( "id", object.getId() );
            }
            else if ( f.getName().equals( "name" ) )
            {
                obj.put( "name", object.getName() );
            }
            else if ( f.getName().equals( "description" ) )
            {
                obj.put( "description", object.getDescription() );
            }
            else if ( f.getName().equals( "host" ) )
            {
                obj.put( "host", object.getHost() );
            }
            else if ( f.getName().equals( "userName" ) )
            {
                try
                {
                    Integer uid = object.getUser();
                    User user = uid == null ? null : UserDBAO.getInstance().getUser( object.getUser() );
                    String userName = user != null ? user.getName() : "-unknown-";
                    obj.put( "userName", userName );
                }
                catch ( UserNotFoundException e )
                {
                    obj.put( "userName", "-unknown-" );
                }
            }
            else if ( f.getName().equals( "allowNonUserTests" ) )
            {
                obj.put( "allowNonUserTests", object.getAllownonusertests() );
            }
            else if ( f.getName().equals( "hastestset" ) )
            {
                String s = object.getHastestset() != null
                    ? String.valueOf(object.getHastestset().getId())
                    : "null";
                obj.put( "hastestset", s );
            }


        }
        return obj.toString();
    }

    public String toJson( List<AgentEntity> objects )
    {
        return Tools.getJsonString( objects, ALL_FIELDS );
    }

    public String toJsonArray( List<AgentEntity> objects, String fields ) throws JSONException
    {
        StringBuilder b = new StringBuilder( "[ " );

        for ( AgentEntity e : objects )
        {
            b.append( toJson( e, fields ) ).append( ", " );
        }
        if ( objects.size() > 0 )
        {
            b.delete( b.length() - 2, b.length() );
        }
        b.append( " ]" );
        return b.toString();
    }

    public AgentEntity toObject( String json ) throws JSONException
    {
        JSONObject obj = new JSONObject( json );
        AgentEntity agentEntity = new AgentEntity();
        int id = (Integer)obj.get( "id" );
        agentEntity.setId( id );
        agentEntity.setName( (String)obj.get( "name" ) );
        agentEntity.setHost( (String)obj.get( "host" ) );
        agentEntity.setDescription( (String)obj.get( "description" ) );
        String uName = (String)obj.get( "userName" );
        User u;
        try
        {
            u = UserDBAO.getInstance().getUser( uName );
            agentEntity.setUser( u.getId() );
        }
        catch ( UserNotFoundException e )
        {
            agentEntity.setUser( null );
        }
        agentEntity.setAllownonusertests( (Boolean)obj.get( "allowNonUserTests" ) );

        return agentEntity;
    }

    public AgentEntity copyValues( AgentEntity to, AgentEntity from )
    {
        // don't set id
        to.setDescription( from.getDescription() );
        to.setHost( from.getHost() );
        to.setUser( from.getUser() );
        to.setName( from.getName() );
        to.setAllownonusertests( from.getAllownonusertests() );
        // all other setters are for collections, not directly editable by user.

        return to;
    }


}